package com.yangjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangjian.bean.Employee;
import com.yangjian.comon.R;
import com.yangjian.dao.EmployeeDao;
import com.yangjian.exception.CustomException;
import com.yangjian.page.PageParameter;
import com.yangjian.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired()
    private EmployeeDao edao;

    /**
     * 登录查询
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {

        LambdaQueryWrapper<Employee> lqw  = new LambdaQueryWrapper<>();

        lqw.eq(Employee::getUsername,employee.getUsername());
        //将前端页面传来的密码进行MD5转化，然后再与后台的数据库进行对比
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lqw.eq(Employee::getPassword,password);
        Employee em = edao.selectOne(lqw);
        //获得员工的在职状态
        Integer status = em.getStatus();
        //判断在职状态
        if (status==0){
            throw new CustomException("该账号已禁用，禁止登录");
        }

        return em;
    }


    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {

        int row = edao.insert(employee);

        return row;
    }



    /**
     * 员工--分页查询
     * @param pageParameter
     * @return
     */
    @Override
    public IPage<Employee> findPage(PageParameter pageParameter) {

        //构建查询对象
        LambdaQueryWrapper<Employee> lqw  = new LambdaQueryWrapper<>();
        //添加查询约束条件--名字
        lqw.like(pageParameter.getName() !=null,Employee::getName ,pageParameter.getName());
        //设置查询多少页，每页多少条
        IPage<Employee> page =new Page<>(pageParameter.getPage(),pageParameter.getPageSize());

        IPage<Employee> employeeIPage = edao.selectPage(page, lqw);

        return employeeIPage;
    }

    /**
     * 修改员工
     *
     * @param employee
     * @return
     */
    @Override
    public int update(Employee employee) {

        int row = edao.updateById(employee);
        return row;
    }


}
